/*
 * @(#)JdbcTemplateBuilder.java 1.0 2000/12/28
 * 
 * Copyright 2000 Thomas Barnekow. All Rights Reserved.
 * 
 * This software is the proprietary information of Thomas Barnekow. Use is
 * subject to license terms.
 */

package jessx.provider.jdbc;

import jess.*;

import tb.provider.*;
import tb.provider.jdbc.*;

/**
 * A {@link Builder}constructing {@link JdbcTemplate}s from {@link Table}s.
 * A {@link Schema}is represented by a {@link GenericCompoundMetaObject}
 * containing a {@link JdbcTemplate}for each of the schema's tables.
 * 
 * @author Thomas Barnekow
 * @version 1.0
 */
public class JdbcTemplateBuilder extends AbstractBuilder {

	private Rete m_engine;
	private CompoundMetaObject m_container;
	private JdbcTemplate m_template;

	public JdbcTemplateBuilder() {
		// TODO: Check whether this is OK.
		// Reason for this is that the Deftemplate constructor was changed
		// and now requires a Rete object
		this(new Rete());
	}

	public JdbcTemplateBuilder(Rete engine) {
		m_engine = engine;
	}

	public void takeSchema(Schema schema) throws Exception {
		if (m_container == null)
			m_container = new GenericCompoundMetaObject(schema.getId());
	}

	public void takeTable(Table table) throws Exception {
		m_template = new JdbcTemplate(table, m_engine);
		if (m_container != null)
			m_container.add(m_template);
	}

	public void takeColumn(Column column) throws Exception {
		m_template.add(new JdbcSlot(column, m_template));
	}

	public MetaObject getResult() {
		if (m_container != null)
			return m_container;
		else
			return m_template;
	}
}